import {
    _getCookie,
    _removeCookie
} from '@/util/cookie'

const TokenKey = 'biwin-token'

export function _getToken() {
    return sessionStorage.getItem(TokenKey)
}

export function _setToken(token) {
    return sessionStorage.setItem(TokenKey, token)
}

export function _removeToken() {
    return sessionStorage.removeItem(TokenKey)
}

export function _clearToken() {
    return sessionStorage.clear()
}

// 判断用户密码是否失效
export function _passwordIsInvalid() {
    const _time = _getCookie('logintime')
    const _nowTime = new Date().getTime()
    const _invalid = _nowTime - _time > 7 * 24 * 60 * 60 * 1000
        // 失效了默认移除密码和用户名 token
    if (_invalid) {
        _removeCookie('username')
        _removeCookie('password')
        _removeCookie('logintime')
        _removeToken()
        sessionStorage.removeItem('lastTime')
    }
    return _invalid
}
